//
//  NavigationController.swift
//  MyWeiBlog
//
//  Created by laichunhui on 15/9/28.
//  Copyright © 2015年 lch. All rights reserved.
//

import UIKit

/**
* 当第一次使用这个类的时候会调用一次
*/
private var once = dispatch_once_t()
class NavigationController: UINavigationController
{
    lazy var btn: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        //image
        btn.setImage(UIImage(named: "common_navbar_return"), forState: .Normal)
        btn.setImage(UIImage(named: "common_navbar_return"), forState: UIControlState.Highlighted)
        
        //title
        btn.setTitle("返回", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(13)
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Highlighted)
        
        btn.frame.size = CGSizeMake(70, 44)
        btn.addTarget(self, action: Selector("back"), forControlEvents: UIControlEvents.TouchUpInside)
        //设置btn整体内容左对齐
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
        btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: -5, bottom: 0, right: 0)
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0)
       
        return btn
    }()
    
    override func pushViewController(viewController: UIViewController, animated: Bool)
    {
        // 如果push进来的不是第一个控制器
        if self.childViewControllers.count > 0
        {
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
            //隐藏tabBar
            self.tabBarController?.hidesBottomBarWhenPushed
            
        }
       super.pushViewController(viewController, animated: true)
    }
    func back()
    {
        self.popViewControllerAnimated(true)
    }
}